from selenium import webdriver
from pageObjects.LoginPage import LoginPage
import time
from appModules.LoginAction import LoginAction
from appModules.AddContactPersonAction import AddContactPerson
from util.ParseExcel import ParseExcel
from config.VarConfig import *

excelObj = ParseExcel()
excelObj.loadWorkBook(dataFilePath)
def launchBrowser():
    driver = webdriver.Chrome()
    driver.get('http://mail.126.com/')
    time.sleep(3)
    return driver

def test126MailAddContacts():
    try:
        userSheet = excelObj.getSheetByName("126账号")
        isExecuteUser = excelObj.getColumn(userSheet,account_isExecute)
        dataBookColumn = excelObj.getColumn(userSheet,account_dataBook)
        print("测试为126邮箱添加联系人执行开始")
        for idx, i in enumerate(isExecuteUser):
            if i.value == "y":
                userRow = excelObj.getRow(userSheet, idx + 1)
                username = userRow[account_username - 1].value
                password = userRow[account_password - 1].value
                print("登录用户名{}，登录密码{}".format(username,password))
                driver = launchBrowser()
                LoginAction.login(driver,username,password)
                time.sleep(3)
                dataBookName = dataBookColumn[idx + 1].value
                dataSheet = excelObj.getSheetByName(dataBookName)
                isExecuteData = excelObj.getColumn(dataSheet, contacts_isExecute)
                contactNum = 0
                isExecuteNum = 0
                for id, data in enumerate(isExecuteData):
                    if data.value == "y":
                        isExecuteNum = isExecuteNum + 1
                        rowContent = excelObj.getRow(dataSheet, id + 1)
                        contactPersonName = rowContent[contacts_contactPersonName - 1].value
                        contactPersonPhone = rowContent[contacts_contactPersonPhone - 1].value
                        contactPersonEmail = rowContent[contacts_contactPersonEmail - 1].value
                        contactPersonComment = rowContent[contacts_contactPersonComment - 1].value
                        assertKeyWord = rowContent[contacts_assertKeyWords - 1].value
                        print(contactPersonName, contactPersonPhone, contactPersonEmail, contactPersonComment,
                              assertKeyWord)
                        AddContactPerson.add(driver,contactPersonName,contactPersonEmail,contactPersonPhone,
                                             contactPersonComment)
                        time.sleep(1)
                        #在工作表中写入添加联系人执行时间
                        excelObj.writeCellCurrentTime(dataSheet,rowNo=id+1,colNo=contacts_runTime)
                        try:
                            assert assertKeyWord in driver.page_source
                        except Exception as e:
                            print(e)
                            excelObj.writeCell(dataSheet,content="fail", rowNo=id + 1,
                                               colNo=contacts_testResult,style="red")
                        else:
                            excelObj.writeCell(dataSheet, content="pass", rowNo=id + 1,
                                               colNo=contacts_testResult, style="green")
                            contactNum +=1
                print("需要添加的联系人数量{}，添加成功的联系人数量{}".format(isExecuteNum,contactNum))
                if isExecuteNum==contactNum:
                    excelObj.writeCell(userSheet,content="pass",rowNo=idx+1,
                                       colNo=account_testResult,style="green")
                else:
                    excelObj.writeCell(userSheet, content="fail", rowNo=idx + 1,
                                       colNo=account_testResult, style="red")
            elif i.value == "n":
                print("用户{}被设置为忽略执行".
                      format(excelObj.getCellValue(userSheet, rowNo=idx + 1, colNo=account_username)))

    except Exception as e:
        print(e)

def testMailLogin():
    try:
        driver = webdriver.Chrome()
        driver.get("http://mail.126.com/")
        LoginAction.login(driver,"wyu0430","19870303")

        time.sleep(3)
        assert "草稿箱" in driver.page_source
    except Exception as e:
        raise e

    finally:
        driver.quit()

if __name__ == '__main__':
    # testMailLogin()
    # print(u"登录126邮箱成功")
    test126MailAddContacts()
